package com.jfeat.am.module.wegou.services.patch.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jfeat.am.core.support.BeanKit;
import com.jfeat.am.core.util.Convert;
import com.jfeat.am.core.util.JsonKit;
import com.jfeat.am.module.systemconfig.services.crud.service.SystemConfigService;
import com.jfeat.am.module.systemconfig.services.persistence.model.SystemConfig;
import com.jfeat.am.module.wegou.services.patch.DealExchangeRateService;
import com.jfeat.am.module.wegou.services.patch.QueryExchangeRateService;
import org.slf4j.Logger;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by Silent-Y on 2018/2/22.
 */
@Service
public class DealExchangeRateServiceImpl implements DealExchangeRateService {

    private Logger logger = org.slf4j.LoggerFactory.getLogger(DealExchangeRateServiceImpl.class);

    @Resource
    private QueryExchangeRateService queryExchangeRateService;
    @Resource
    private SystemConfigService systemConfigService;

    @Override
    public Integer saveExchangeRate() throws Exception{
        ResponseEntity<String> responseEntity = queryExchangeRateService.getExchangeRate();
        logger.info("通过第三方api获取到的responseEntity" + responseEntity);
        String body = responseEntity.getBody();
        Map<String, String> map = dealExchangeRate(body);
        return insertExchangeRate(map);
    }

    public Map<String, String> dealExchangeRate(String s) {
        JSONObject jsonObject = JSON.parseObject(s);
        Map<String, String> map = Maps.newHashMap();

        if (jsonObject.getInteger("resultcode") == 200){
            JSONArray result = jsonObject.getJSONArray("result");
            for (int i = 0; i < result.size(); i++) {
                JSONObject item = result.getJSONObject(i);
                Set<String> keys = item.keySet();
                for (String key : keys) {
                    JSONObject data = item.getJSONObject(key);
                    String name = data.getString("name");
                    String bankConversionPri = data.getString("bankConversionPri");
                    map.put(name,bankConversionPri);
                }
            }
        }
        return map;
    }

    public Integer insertExchangeRate(Map<String, String> map) {
        Integer affected = 0;
        for (Map.Entry<String,String> entry:map.entrySet()){
            String key = entry.getKey();
            String value = entry.getValue();
            SystemConfig systemConfig = systemConfigService.getSystemConfigByKey(key.toString());
            if (systemConfig == null) {
                SystemConfig systemConfigNew = new SystemConfig();
                systemConfigNew.setDataKey(key);
                systemConfigNew.setDataValue(value);
                affected += systemConfigService.createMaster(systemConfigNew);
            } else {
                systemConfig.setDataValue(value);
                affected += systemConfigService.updateMaster(systemConfig);
            }
        }
        return affected;
    }


    @Override
    public SystemConfig getSystemConfigByKey(String key) {
        return systemConfigService.getSystemConfigByKey(key);
    }
}
